package com.qf.business.system.controller;

import com.qf.business.system.power.Auth;
import com.qf.business.system.service.DepartmentService;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import com.qf.data.system.entity.Department;
import com.qf.data.system.vo.input.DepartmentInput;
import com.qf.data.system.vo.output.DepListOutput;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@RestController
@RequestMapping("/dep")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 查询部门列表
     * @return
     */
    @Auth(mustLogin = false)
    @RequestMapping("/list")
    public R depList(){
        //
        List<Department> departments = departmentService.list();
        //entity -> output
        List<DepListOutput> outList = departments.stream()
                .map(department -> {
                    DepListOutput out = new DepListOutput();
                    BeanUtils.copyProperties(department, out);
                    return out;
                }).collect(Collectors.toList());
        //返回部门列表
        return Rs.create(outList);
    }

    /**
     * 新增部门
     * @return
     */
    @RequestMapping("/insert")
    public R insert(@Validated DepartmentInput depinput){
        //将input -> entity
        Department department = new Department();
        BeanUtils.copyProperties(depinput, department);
        //进行保存
        departmentService.save(department);
        return Rs.create("succ");
    }
}
